package com.pencilisland.pay.app.mp.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pencilisland.pay.api.bean.constant.OrderConstant;
import com.pencilisland.pay.api.bean.mp.app.MessageRequest;
import com.pencilisland.pay.api.bean.mp.app.MessageResponse;
import com.pencilisland.pay.api.bean.mp.app.WxPayCancelREQ;
import com.pencilisland.pay.api.bean.mp.app.WxPayCancelRES;
import com.pencilisland.pay.api.bean.mp.core.UpdateOrderREQ;
import com.pencilisland.pay.api.bean.mp.core.UpdateOrderRES;
import com.pencilisland.pay.app.mp.service.MBService;
/**
* 报文逻辑处理.微信用户取消支付
*
* @author breeze
*/
import com.pencilisland.pay.app.service.RestService;
@Service("MB_wxPayCancel")
public class  WxPayCancelMB implements MBService {
	
	Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	ObjectMapper objectMapper;
	@Autowired
	RestService restService;

	@Override
	public MessageResponse process(MessageRequest request) {
		WxPayCancelREQ requestBean = getRequestBody(request);
		
		if(StringUtils.isEmpty(requestBean.getOrderNo())){
			log.info("取消订单号为空");
		}
		
		UpdateOrderREQ updateOrderReq = new UpdateOrderREQ();
		List<UpdateOrderREQ.ElementOrders> updateOrders = new ArrayList<UpdateOrderREQ.ElementOrders>();
		UpdateOrderREQ.ElementOrders updateOrder = new UpdateOrderREQ.ElementOrders();
		updateOrder.setOrderNo(requestBean.getOrderNo());
		updateOrder.setFrom("APP");
		updateOrder.setStatus(OrderConstant.STATUS_4);
		updateOrders.add(updateOrder);
		updateOrderReq.setOrders(updateOrders);
		
		restService.sendToCoreMP("updateOrder", updateOrderReq, UpdateOrderRES.class);
		
		WxPayCancelRES res= new WxPayCancelRES(true,null,"OK");
		return res;
	}
	
	public WxPayCancelREQ getRequestBody(MessageRequest request){
		return objectMapper.convertValue(request.getBody(), WxPayCancelREQ.class);
	}

}
